home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr54 / bison118.zip / CONFLICT.C < prev    next >
C/C++ Source or Header  |  1993-06-01  |  16KB  |  774 lines

  1. /* Find and resolve or report look-ahead conflicts for bison,
  2.    Copyright (C) 1984, 1989 Free Software Foundation, Inc.
  3.  
  4. This file is part of Bison, the GNU Compiler Compiler.
  5.  
  6. Bison is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2, or (at your option)
  9. any later version.
  10.  
  11. Bison is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with Bison; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. #ifdef _AIX
  21.  #pragma alloca
  22. #endif
  23. #include <stdio.h>
  24. #include "system.h"
  25. #include "machine.h"
  26. #include "new.h"
  27. #include "files.h"
  28. #include "gram.h"
  29. #include "state.h"
  30.  
  31.  
  32. #if defined(STDC_HEADERS) || defined(__GNU_LIBRARY__)
  33. #define bcopy(s, d, n) memcpy ((d), (s), (n))
  34. #else
  35. #ifdef USG
  36. #include <memory.h>
  37. #define bcopy(src, dst, num) memcpy((dst), (src), (num))
  38. #endif
  39. #endif
  40.  
  41. #ifdef __GNUC__
  42. #define alloca __builtin_alloca
  43. #else
  44. #ifdef sparc
  45. #include <alloca.h>
  46. #else
  47. #ifndef _AIX
  48. extern char *alloca ();
  49. #endif
  50. #endif
  51. #endif
  52.  
  53. extern char **tags;
  54. extern int tokensetsize;
  55. extern char *consistent;
  56. extern short *accessing_symbol;
  57. extern shifts **shift_table;
  58. extern unsigned *LA;
  59. extern short *LAruleno;
  60. extern short *lookaheads;
  61. extern int verboseflag;
  62.  
  63. void set_conflicts();
  64. void resolve_sr_conflict();
  65. void flush_shift();
  66. void log_resolution();
  67. void total_conflicts();
  68. void count_sr_conflicts();
  69. void count_rr_conflicts();
  70.  
  71. char any_conflicts;
  72. char *conflicts;
  73. errs **err_table;
  74. int expected_conflicts;
  75.  
  76.  
  77. static unsigned *shiftset;
  78. static unsigned *lookaheadset;
  79. static int src_total;
  80. static int rrc_total;
  81. static int src_count;
  82. static int rrc_count;
  83.  
  84.  
  85. void
  86. initialize_conflicts()
  87. {
  88.   register int i;
  89. /*  register errs *sp; JF unused */
  90.  
  91.   conflicts = NEW2(nstates, char);
  92.   shiftset = NEW2(tokensetsize, unsigned);
  93.   lookaheadset = NEW2(tokensetsize, unsigned);
  94.  
  95.   err_table = NEW2(nstates, errs *);
  96.  
  97.   any_conflicts = 0;
  98.  
  99.   for (i = 0; i < nstates; i++)
  100.     set_conflicts(i);
  101. }
  102.  
  103.  
  104. void
  105. set_conflicts(state)
  106. int state;
  107. {
  108.   register int i;
  109.   register int k;
  110.   register shifts *shiftp;
  111.   register unsigned *fp2;
  112.   register unsigned *fp3;
  113.   register unsigned *fp4;
  114.   register unsigned *fp1;
  115.   register int symbol;
  116.  
  117.   if (consistent[state]) return;
  118.  
  119.   for (i = 0; i < tokensetsize; i++)
  120.     lookaheadset[i] = 0;
  121.  
  122.   shiftp = shift_table[state];
  123.   if (shiftp)
  124.     {
  125.       k = shiftp->nshifts;
  126.       for (i = 0; i < k; i++)
  127.     {
  128.       symbol = accessing_symbol[shiftp->shifts[i]];
  129.       if (ISVAR(symbol)) break;
  130.       SETBIT(lookaheadset, symbol);
  131.     }
  132.     }
  133.  
  134.   k = lookaheads[state + 1];
  135.   fp4 = lookaheadset + tokensetsize;
  136.  
  137.   /* loop over all rules which require lookahead in this state */
  138.   /* first check for shift-reduce conflict, and try to resolve using precedence  */
  139.  
  140.   for (i = lookaheads[state]; i < k; i++)
  141.     if (rprec[LAruleno[i]])
  142.       {
  143.     fp1 = LA + i * tokensetsize;
  144.     fp2 = fp1;
  145.     fp3 = lookaheadset;
  146.   
  147.     while (fp3 < fp4)
  148.       {
  149.         if (*fp2++ & *fp3++)
  150.           {
  151.         resolve_sr_conflict(state, i);
  152.         break;
  153.           }
  154.       }
  155.       }
  156.  
  157.   /* loop over all rules which require lookahead in this state */
  158.   /* Check for conflicts not resolved above.  */
  159.  
  160.   for (i = lookaheads[state]; i < k; i++)
  161.     {
  162.       fp1 = LA + i * tokensetsize;
  163.       fp2 = fp1;
  164.       fp3 = lookaheadset;
  165.  
  166.       while (fp3 < fp4)
  167.     {
  168.       if (*fp2++ & *fp3++)
  169.         {
  170.           conflicts[state] = 1;
  171.           any_conflicts = 1;
  172.         }
  173.     }
  174.  
  175.       fp2 = fp1;
  176.       fp3 = lookaheadset;
  177.  
  178.       while (fp3 < fp4)
  179.     *fp3++ |= *fp2++;
  180.     }
  181. }
  182.  
  183.  
  184.  
  185. /* Attempt to resolve shift-reduce conflict for one rule
  186. by means of precedence declarations.
  187. It has already been checked that the rule has a precedence.
  188. A conflict is resolved by modifying the shift or reduce tables
  189. so that there is no longer a conflict.  */
  190.  
  191. void
  192. resolve_sr_conflict(state, lookaheadnum)
  193. int state;
  194. int lookaheadnum;
  195. {
  196.   register int i;
  197.   register int mask;
  198.   register unsigned *fp1;
  199.   register unsigned *fp2;
  200.   register int redprec;
  201.   errs *errp = (errs *) alloca (sizeof(errs) + ntokens * sizeof(short));
  202.   short *errtokens = errp->errs;
  203.  
  204.   /* find the rule to reduce by to get precedence of reduction  */
  205.   redprec = rprec[LAruleno[lookaheadnum]];
  206.  
  207.   mask = 1;
  208.   fp1 = LA + lookaheadnum * tokensetsize;
  209.   fp2 = lookaheadset;
  210.   for (i = 0; i < ntokens; i++)
  211.     {
  212.       if ((mask & *fp2 & *fp1) && sprec[i])
  213.     /* Shift-reduce conflict occurs for token number i
  214.        and it has a precedence.
  215.        The precedence of shifting is that of token i.  */
  216.     {
  217.       if (sprec[i] < redprec)
  218.         {
  219.           if (verboseflag) log_resolution(state, lookaheadnum, i, "reduce");
  220.           *fp2 &= ~mask;  /* flush the shift for this token */
  221.           flush_shift(state, i);
  222.         }
  223.       else if (sprec[i] > redprec)
  224.         {
  225.           if (verboseflag) log_resolution(state, lookaheadnum, i, "shift");
  226.           *fp1 &= ~mask;  /* flush the reduce for this token */
  227.         }
  228.       else
  229.         {
  230.           /* Matching precedence levels.
  231.          For left association, keep only the reduction.
  232.          For right association, keep only the shift.
  233.          For nonassociation, keep neither.  */
  234.  
  235.           switch (sassoc[i])
  236.         {
  237.  
  238.         case RIGHT_ASSOC:
  239.               if (verboseflag) log_resolution(state, lookaheadnum, i, "shift");
  240.           break;
  241.  
  242.         case LEFT_ASSOC:
  243.               if (verboseflag) log_resolution(state, lookaheadnum, i, "reduce");
  244.           break;
  245.  
  246.         case NON_ASSOC:
  247.               if (verboseflag) log_resolution(state, lookaheadnum, i, "an error");
  248.           break;
  249.         }
  250.  
  251.           if (sassoc[i] != RIGHT_ASSOC)
  252.         {
  253.           *fp2 &= ~mask;  /* flush the shift for this token */
  254.           flush_shift(state, i);
  255.         }
  256.           if (sassoc[i] != LEFT_ASSOC)
  257.             {
  258.           *fp1 &= ~mask;  /* flush the reduce for this token */
  259.         }
  260.           if (sassoc[i] == NON_ASSOC)
  261.         {
  262.           /* Record an explicit error for this token.  */
  263.           *errtokens++ = i;
  264.         }
  265.         }
  266.     }
  267.  
  268.       mask <<= 1;
  269.       if (mask == 0)
  270.     {
  271.       mask = 1;
  272.       fp2++;  fp1++;
  273.     }
  274.     }
  275.   errp->nerrs = errtokens - errp->errs;
  276.   if (errp->nerrs)
  277.     {
  278.       /* Some tokens have been explicitly made errors.  Allocate
  279.      a permanent errs structure for this state, to record them.  */
  280.       i = (char *) errtokens - (char *) errp;
  281.       err_table[state] = (errs *) mallocate ((unsigned int)i);
  282.       bcopy (errp, err_table[state], i);
  283.     }
  284.   else
  285.     err_table[state] = 0;
  286. }
  287.  
  288.  
  289.  
  290. /* turn off the shift recorded for the specified token in the specified state.
  291. Used when we resolve a shift-reduce conflict in favor of the reduction.  */
  292.  
  293. void
  294. flush_shift(state, token)
  295. int state;
  296. int token;
  297. {
  298.   register shifts *shiftp;
  299.   register int k, i;
  300. /*  register unsigned symbol; JF unused */
  301.  
  302.   shiftp = shift_table[state];
  303.  
  304.   if (shiftp)
  305.     {
  306.       k = shiftp->nshifts;
  307.       for (i = 0; i < k; i++)
  308.     {
  309.       if (shiftp->shifts[i] && token == accessing_symbol[shiftp->shifts[i]])
  310.         (shiftp->shifts[i]) = 0;
  311.     }
  312.     }
  313. }
  314.  
  315.  
  316. void
  317. log_resolution(state, LAno, token, resolution)
  318. int state, LAno, token;
  319. char *resolution;
  320. {
  321.   fprintf(foutput,
  322.       "Conflict in state %d between rule %d and token %s resolved as %s.\n",
  323.       state, LAruleno[LAno], tags[token], resolution);
  324. }
  325.  
  326.  
  327. void
  328. conflict_log()
  329. {
  330.   register int i;
  331.  
  332.   src_total = 0;
  333.   rrc_total = 0;
  334.  
  335.   for (i = 0; i < nstates; i++)
  336.     {
  337.       if (conflicts[i])
  338.     {
  339.       count_sr_conflicts(i);
  340.       count_rr_conflicts(i);
  341.       src_total += src_count;
  342.       rrc_total += rrc_count;
  343.     }
  344.     }
  345.  
  346.   total_conflicts();
  347. }
  348.   
  349.  
  350. void
  351. verbose_conflict_log()
  352. {
  353.   register int i;
  354.  
  355.   src_total = 0;
  356.   rrc_total = 0;
  357.  
  358.   for (i = 0; i < nstates; i++)
  359.     {
  360.       if (conflicts[i])
  361.     {
  362.       count_sr_conflicts(i);
  363.       count_rr_conflicts(i);
  364.       src_total += src_count;
  365.       rrc_total += rrc_count;
  366.  
  367.       fprintf(foutput, "State %d contains", i);
  368.  
  369.       if (src_count == 1)
  370.         fprintf(foutput, " 1 shift/reduce conflict");
  371.       else if (src_count > 1)
  372.         fprintf(foutput, " %d shift/reduce conflicts", src_count);
  373.  
  374.       if (src_count > 0 && rrc_count > 0)
  375.         fprintf(foutput, " and");
  376.  
  377.       if (rrc_count == 1)
  378.         fprintf(foutput, " 1 reduce/reduce conflict");
  379.       else if (rrc_count > 1)
  380.         fprintf(foutput, " %d reduce/reduce conflicts", rrc_count);
  381.  
  382.       putc('.', foutput);
  383.       putc('\n', foutput);
  384.     }
  385.     }
  386.  
  387.   total_conflicts();
  388. }
  389.  
  390.  
  391. void
  392. total_conflicts()
  393. {
  394.   extern int fixed_outfiles;
  395.  
  396.   if (src_total == expected_conflicts && rrc_total == 0)
  397.     return;
  398.  
  399.   if (fixed_outfiles)
  400.     {
  401.       /* If invoked under the name `yacc', use the output format
  402.      specified by POSIX.  */
  403.       fprintf(stderr, "conflicts: ");
  404.       if (src_total > 0)
  405.     fprintf(stderr, " %d shift/reduce", src_total);
  406.       if (src_total > 0 && rrc_total > 0)
  407.     fprintf(stderr, ",");
  408.       if (rrc_total > 0)
  409.     fprintf(stderr, " %d reduce/reduce", rrc_total);
  410.       putc('\n', stderr);
  411.     }
  412.   else
  413.     {
  414.       fprintf(stderr, "%s contains", infile);
  415.  
  416.       if (src_total == 1)
  417.     fprintf(stderr, " 1 shift/reduce conflict");
  418.       else if (src_total > 1)
  419.     fprintf(stderr, " %d shift/reduce conflicts", src_total);
  420.  
  421.       if (src_total > 0 && rrc_total > 0)
  422.     fprintf(stderr, " and");
  423.  
  424.       if (rrc_total == 1)
  425.     fprintf(stderr, " 1 reduce/reduce conflict");
  426.       else if (rrc_total > 1)
  427.     fprintf(stderr, " %d reduce/reduce conflicts", rrc_total);
  428.  
  429.       putc('.', stderr);
  430.       putc('\n', stderr);
  431.     }
  432. }
  433.  
  434.  
  435. void
  436. count_sr_conflicts(state)
  437. int state;
  438. {
  439.   register int i;
  440.   register int k;
  441.   register int mask;
  442.   register shifts *shiftp;
  443.   register unsigned *fp1;
  444.   register unsigned *fp2;
  445.   register unsigned *fp3;
  446.   register int symbol;
  447.  
  448.   src_count = 0;
  449.  
  450.   shiftp = shift_table[state];
  451.   if (!shiftp) return;
  452.  
  453.   for (i = 0; i < tokensetsize; i++)
  454.     {
  455.       shiftset[i] = 0;
  456.       lookaheadset[i] = 0;
  457.     }
  458.  
  459.   k = shiftp->nshifts;
  460.   for (i = 0; i < k; i++)
  461.     {
  462.       if (! shiftp->shifts[i]) continue;
  463.       symbol = accessing_symbol[shiftp->shifts[i]];
  464.       if (ISVAR(symbol)) break;
  465.       SETBIT(shiftset, symbol);
  466.     }
  467.  
  468.   k = lookaheads[state + 1];
  469.   fp3 = lookaheadset + tokensetsize;
  470.  
  471.   for (i = lookaheads[state]; i < k; i++)
  472.     {
  473.       fp1 = LA + i * tokensetsize;
  474.       fp2 = lookaheadset;
  475.  
  476.       while (fp2 < fp3)
  477.     *fp2++ |= *fp1++;
  478.     }
  479.  
  480.   fp1 = shiftset;
  481.   fp2 = lookaheadset;
  482.  
  483.   while (fp2 < fp3)
  484.     *fp2++ &= *fp1++;
  485.  
  486.   mask = 1;
  487.   fp2 = lookaheadset;
  488.   for (i = 0; i < ntokens; i++)
  489.     {
  490.       if (mask & *fp2)
  491.     src_count++;
  492.  
  493.       mask <<= 1;
  494.       if (mask == 0)
  495.     {
  496.       mask = 1;
  497.       fp2++;
  498.     }
  499.     }
  500. }
  501.  
  502.  
  503. void
  504. count_rr_conflicts(state)
  505. int state;
  506. {
  507.   register int i;
  508.   register int j;
  509.   register int count;
  510.   register unsigned mask;
  511.   register unsigned *baseword;
  512.   register unsigned *wordp;
  513.   register int m;
  514.   register int n;
  515.  
  516.   rrc_count = 0;
  517.  
  518.   m = lookaheads[state];
  519.   n = lookaheads[state + 1];
  520.  
  521.   if (n - m < 2) return;
  522.  
  523.   mask = 1;
  524.   baseword = LA + m * tokensetsize;
  525.   for (i = 0; i < ntokens; i++)
  526.     {
  527.       wordp = baseword;
  528.  
  529.       count = 0;
  530.       for (j = m; j < n; j++)
  531.     {
  532.       if (mask & *wordp)
  533.         count++;
  534.  
  535.       wordp += tokensetsize;
  536.     }
  537.  
  538.       if (count >= 2) rrc_count++;
  539.  
  540.       mask <<= 1;
  541.       if (mask == 0)
  542.     {
  543.       mask = 1;
  544.       baseword++;
  545.     }
  546.     }
  547. }
  548.  
  549.  
  550. void
  551. print_reductions(state)
  552. int state;
  553. {
  554.   register int i;
  555.   register int j;
  556.   register int k;
  557.   register unsigned *fp1;
  558.   register unsigned *fp2;
  559.   register unsigned *fp3;
  560.   register unsigned *fp4;
  561.   register int rule;
  562.   register int symbol;
  563.   register unsigned mask;
  564.   register int m;
  565.   register int n;
  566.   register int default_LA;
  567.   register int default_rule;
  568.   register int cmax;
  569.   register int count;
  570.   register shifts *shiftp;
  571.   register errs *errp;
  572.   int nodefault = 0;
  573.  
  574.   for (i = 0; i < tokensetsize; i++)
  575.     shiftset[i] = 0;
  576.  
  577.   shiftp = shift_table[state];
  578.   if (shiftp)
  579.     {
  580.       k = shiftp->nshifts;
  581.       for (i = 0; i < k; i++)
  582.     {
  583.       if (! shiftp->shifts[i]) continue;
  584.       symbol = accessing_symbol[shiftp->shifts[i]];
  585.       if (ISVAR(symbol)) break;
  586.       /* if this state has a shift for the error token,
  587.          don't use a default rule.  */
  588.       if (symbol == error_token_number) nodefault = 1;
  589.       SETBIT(shiftset, symbol);
  590.     }
  591.     }
  592.  
  593.   errp = err_table[state];
  594.   if (errp)
  595.     {
  596.       k = errp->nerrs;
  597.       for (i = 0; i < k; i++)
  598.     {
  599.       if (! errp->errs[i]) continue;
  600.       symbol = errp->errs[i];
  601.       SETBIT(shiftset, symbol);
  602.     }
  603.     }
  604.  
  605.   m = lookaheads[state];
  606.   n = lookaheads[state + 1];
  607.  
  608.   if (n - m == 1 && ! nodefault)
  609.     {
  610.       default_rule = LAruleno[m];
  611.  
  612.       fp1 = LA + m * tokensetsize;
  613.       fp2 = shiftset;
  614.       fp3 = lookaheadset;
  615.       fp4 = lookaheadset + tokensetsize;
  616.  
  617.       while (fp3 < fp4)
  618.     *fp3++ = *fp1++ & *fp2++;
  619.  
  620.       mask = 1;
  621.       fp3 = lookaheadset;
  622.  
  623.       for (i = 0; i < ntokens; i++)
  624.     {
  625.       if (mask & *fp3)
  626.         fprintf(foutput, "    %-4s\t[reduce using rule %d (%s)]\n",
  627.             tags[i], default_rule, tags[rlhs[default_rule]]);
  628.  
  629.       mask <<= 1;
  630.       if (mask == 0)
  631.         {
  632.           mask = 1;
  633.           fp3++;
  634.         }
  635.     }
  636.  
  637.       fprintf(foutput, "    $default\treduce using rule %d (%s)\n\n",
  638.           default_rule, tags[rlhs[default_rule]]);
  639.     }
  640.   else if (n - m >= 1)
  641.     {
  642.       cmax = 0;
  643.       default_LA = -1;
  644.       fp4 = lookaheadset + tokensetsize;
  645.  
  646.       if (! nodefault)
  647.     for (i = m; i < n; i++)
  648.       {
  649.         fp1 = LA + i * tokensetsize;
  650.         fp2 = shiftset;
  651.         fp3 = lookaheadset;
  652.   
  653.         while (fp3 < fp4)
  654.           *fp3++ = *fp1++ & ( ~ (*fp2++));
  655.   
  656.         count = 0;
  657.         mask = 1;
  658.         fp3 = lookaheadset;
  659.         for (j = 0; j < ntokens; j++)
  660.           {
  661.         if (mask & *fp3)
  662.           count++;
  663.   
  664.         mask <<= 1;
  665.         if (mask == 0)
  666.           {
  667.             mask = 1;
  668.             fp3++;
  669.           }
  670.           }
  671.   
  672.         if (count > cmax)
  673.           {
  674.         cmax = count;
  675.         default_LA = i;
  676.         default_rule = LAruleno[i];
  677.           }
  678.   
  679.         fp2 = shiftset;
  680.         fp3 = lookaheadset;
  681.   
  682.         while (fp3 < fp4)
  683.           *fp2++ |= *fp3++;
  684.       }
  685.  
  686.       for (i = 0; i < tokensetsize; i++)
  687.         shiftset[i] = 0;
  688.  
  689.       if (shiftp)
  690.         {
  691.           k = shiftp->nshifts;
  692.           for (i = 0; i < k; i++)
  693.         {
  694.           if (! shiftp->shifts[i]) continue;
  695.           symbol = accessing_symbol[shiftp->shifts[i]];
  696.           if (ISVAR(symbol)) break;
  697.           SETBIT(shiftset, symbol);
  698.         }
  699.         }
  700.  
  701.       mask = 1;
  702.       fp1 = LA + m * tokensetsize;
  703.       fp2 = shiftset;
  704.       for (i = 0; i < ntokens; i++)
  705.     {
  706.       int defaulted = 0;
  707.  
  708.       if (mask & *fp2)
  709.         count = 1;
  710.       else
  711.         count = 0;
  712.  
  713.       fp3 = fp1;
  714.       for (j = m; j < n; j++)
  715.         {
  716.           if (mask & *fp3)
  717.         {
  718.           if (count == 0)
  719.             {
  720.               if (j != default_LA)
  721.             {
  722.               rule = LAruleno[j];
  723.               fprintf(foutput, "    %-4s\treduce using rule %d (%s)\n",
  724.                   tags[i], rule, tags[rlhs[rule]]);
  725.             }
  726.               else defaulted = 1;
  727.  
  728.               count++;
  729.             }
  730.           else
  731.             {
  732.               if (defaulted)
  733.             {
  734.               rule = LAruleno[default_LA];
  735.               fprintf(foutput, "    %-4s\treduce using rule %d (%s)\n",
  736.                   tags[i], rule, tags[rlhs[rule]]);
  737.               defaulted = 0;
  738.             }
  739.               rule = LAruleno[j];
  740.               fprintf(foutput, "    %-4s\t[reduce using rule %d (%s)]\n",
  741.                   tags[i], rule, tags[rlhs[rule]]);
  742.             }
  743.         }
  744.  
  745.           fp3 += tokensetsize;
  746.         }
  747.  
  748.       mask <<= 1;
  749.       if (mask == 0)
  750.         {
  751.           mask = 1;
  752.           fp1++;
  753.         }
  754.     }
  755.  
  756.       if (default_LA >= 0)
  757.     {
  758.       fprintf(foutput, "    $default\treduce using rule %d (%s)\n",
  759.           default_rule, tags[rlhs[default_rule]]);
  760.     }
  761.  
  762.       putc('\n', foutput);
  763.     }
  764. }
  765.  
  766.  
  767. void
  768. finalize_conflicts()
  769. {
  770.   FREE(conflicts);
  771.   FREE(shiftset);
  772.   FREE(lookaheadset);
  773. }
  774.